﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using NS_Controls;
using System.Collections;
using AjaxControlToolkit;
using System.Drawing;
using System.Web.UI.HtmlControls;

public partial class Report_Template : ReportBase
{
    protected string sHeader = string.Empty;
    protected string sFooter = string.Empty;
    protected string sTitle = string.Empty;

	#region ViewState storage
	public string strProcedureName
    {
        get { return (string)(this.ViewState["strProcedureName"] ?? ""); }
        set { this.ViewState["strProcedureName"] = value; }
    }

    public string strDatabaseName
    {
        get { return (string)(this.ViewState["strDatabaseName"] ?? ""); }
        set { this.ViewState["strDatabaseName"] = value; }
    }

    public string strDateStart
    {
        get { return (string)(this.ViewState["strDateStart"] ?? ""); }
        set { this.ViewState["strDateStart"] = value; }
    }

    public string strDateEnd
    {
        get { return (string)(this.ViewState["strDateEnd"] ?? ""); }
        set { this.ViewState["strDateEnd"] = value; }
    }

    public string strHeader
    {
        get { return (string)(this.ViewState["strHeader"] ?? ""); }
        set { this.ViewState["strHeader"] = value; }
    }

    public string strFooter
    {
        get { return (string)(this.ViewState["strFooter"] ?? ""); }
        set { this.ViewState["strFooter"] = value; }
    }

    public string strTitle
    {
        get { return (string)(this.ViewState["strTitle"] ?? ""); }
        set { this.ViewState["strTitle"] = value; }
	}

	public System.Collections.Specialized.NameValueCollection arrayParams
	{
		get { return (System.Collections.Specialized.NameValueCollection)(this.ViewState["arrayParams"] ?? ""); }
		set { this.ViewState["arrayParams"] = value; }
	}
	#endregion
	#region Session storage
	public string[] ColumnName
    {
        get { return (string[])(this.Page.Session[this.ClientID + "ColumnName"] ?? new string[] { }); }
        set { this.Page.Session[this.ClientID + "ColumnName"] = value; }
    }

    public string[] ColumnSpanEnumeration
    {
        get { return (string[])(this.Page.Session[this.ClientID + "ColumnSpanEnumeration"] ?? new string[] { }); }
        set { this.Page.Session[this.ClientID + "ColumnSpanEnumeration"] = value; }
    }

    public string[] ColumnSpanTitle
    {
        get { return (string[])(this.Page.Session[this.ClientID + "ColumnSpanTitle"] ?? new string[] { }); }
        set { this.Page.Session[this.ClientID + "ColumnSpanTitle"] = value; }
    }

    public string[] ColumnSum
    {
        get { return (string[])(this.Page.Session[this.ClientID + "ColumnSum"] ?? new string[] { }); }
        set { this.Page.Session[this.ClientID + "ColumnSum"] = value; }
    }

    public string[] ColumnFormula
    {
        get { return (string[])(this.Page.Session[this.ClientID + "ColumnFormula"] ?? new string[] { }); }
        set { this.Page.Session[this.ClientID + "ColumnFormula"] = value; }
    }

    public string[] HiddenColumn
    {
        get { return (string[])(this.Page.Session[this.ClientID + "HiddenColumn"] ?? new string[] { }); }
        set { this.Page.Session[this.ClientID + "HiddenColumn"] = value; }
	}
	#endregion

	private Dictionary<int, int> headerInfo = null;
	public Dictionary<int, int> HeaderInfo
	{
		get
		{
			if (headerInfo == null)
			{
				headerInfo = new Dictionary<int, int>();
				string[] spans = ColumnSpanEnumeration;
				int delay = 0, max = -1;
				foreach (string span in spans)
					if (!string.IsNullOrEmpty(span))
					{
						if (span.Contains(':'))
						{
							string[] parts = span.Split(':');
							int startAt = int.Parse(parts[0]) - 1;
							if (startAt <= max)
								throw new ApplicationException("Ordinea coloanelor defalcate este incorecta!");
							startAt += delay;
							max = startAt;
							int length = int.Parse(parts[1]);
							delay += length - 1;
							headerInfo.Add(startAt, length);
							for (int x = 1; x < length; ++x)
								headerInfo.Add(startAt + x, 0);
						}
						else
						{
							int value = int.Parse(span) - 1;
							if (value <= max)
								throw new ApplicationException("Ordinea coloanelor defalcate este incorecta!");
							value += delay;
							max = value;
							++delay;
							headerInfo.Add(value, 2);
							headerInfo.Add(value + 1, 0);
						}
					}
			}
			return headerInfo;
		}
	}

	public void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    private void LoadData()
    {
        try
        {
            if (string.IsNullOrEmpty(this.strDateStart))
                this.strDateStart = DateTime.Now.ToString();
            if (string.IsNullOrEmpty(this.strDateEnd))
                this.strDateEnd = DateTime.Now.ToString();
            //DataSet ds_Select = Hipocrate.DB.DBConnection.GetData(strProcedureName, new string[] { "StartDate", "EndDate" }, new object[] { Convert.ToDateTime(this.strDateStart), Convert.ToDateTime(this.strDateEnd) }, false);
			DataSet ds_Select = ExportTools.CommonExport.GetCustomData(strProcedureName, arrayParams);
            DataSet ds_Reports = Hipocrate.DB.DBConnection.GetData("ReportsCustom_Select", new string[] { "ID" }, new object[] { this.strReportID });

			HtmlGenericControl emptyTable;
			HtmlGenericControl result = ExportTools.CommonExport.CreateHtmlObject(ds_Reports, ds_Select, out sHeader, out sTitle, out sFooter, out emptyTable);

			foreach (Control c in result.Controls) if (string.Equals(c.ID, "EmptyTablePlaceholder")) { c.Controls.Add(emptyTable); break; }
			ph.Controls.Add(result);

			ExportTools.CommonExport.UnPackCustomMarkup(ref sHeader, ref sTitle, ref sFooter, arrayParams);
        }
        catch (Exception ex)
        {
            lblMessages.Text = ex.Message + "<!-- " + ex.ToString() + " -->";
        }
	}

	#region pagination
	public int CurrentPage
    {
        get
        {
            object o = this.ViewState["_CurrentPage"];
            if (o == null)
                return 0;
            else
                return (int)o;
        }

        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    }

    protected void cmdPrev_Click(object sender, System.EventArgs e)
    {
        CurrentPage -= 1;
        LoadData();
    }

    protected void cmdNext_Click(object sender, System.EventArgs e)
    {
        CurrentPage += 1;
        LoadData();
	}
	#endregion

	public override void bindReportView(DataView dv)
    {

    }

    public override void saveReportView(ref DataView dv)
    {

    }
}
